import api from '../../api';
import { message } from 'antd';

export default {
  namespace: 'investmentTipsM',
  state: {},
  effects: {
    // 获取定价复核、估值核对、压力测试报告、现金管理报告未读消息
    *getUnreadList({ payload, callback }, { call, put }) {
      const res = yield call(api.investmentTips.getUnreadList, payload);
      if (res && res.code == 0) {
        callback(res.data);
      } else {
        callback();
        message.error(`获取未读消息列表失败，失败原因：${res?.msg}`);
      }
    },

    // 未交收管理--交易交收
    *getUndeliveredList({ payload, callback }, { call, put, select }) {
      const res = yield call(api.bookkeepingSettle.getList, payload);
      if (res && res.success) {
        callback(res.data);
      } else {
        callback();
        message.error(res?.message);
      }
    },

    // 未交收管理--行为交收
    *getUndeliveredList2({ payload, callback }, { call, put, select }) {
      const res = yield call(api.bookkeepingSettle.getBehavioeList, payload);
      if (res && res.success) {
        callback(res.data);
      } else {
        callback();
        message.error(res?.message);
      }
    },

    // 获取负面舆情数据
    *getNegativeData({ payload, callback }, { call, put }) {
      const res = yield call(api.investmentTips.getNegativeData, payload);
      let columns = [
        {
          title: '产品名称',
          key: 'productName',
          render: (text, record) => {
            return record.productName + '-' + record.productCode;
          },
        },
        {
          title: '标的名称',
          key: 'securityName',
          render: (text, record) => {
            return record.securityName + '-' + record.securityCode;
          },
        },
        {
          title: '持仓占比',
          key: 'holdRatio',
          type: 'rate',
          width: 80,
        },
        {
          title: '新闻标题',
          key: 'newsTitle',
        },
      ];
      if (res && res.code === 0) {
        callback(columns, res.data);
      } else {
        callback(columns, []);
        message.error(res?.msg || '获取负面舆情数据失败');
      }
    },

    // 评级下调
    *getRatingData({ payload, callback }, { call, put }) {
      const res = yield call(api.investmentTips.getRatingData, payload);
      let columns = [
        {
          title: '产品名称',
          key: 'productName',
          render: (text, record) => {
            return record.productName + '-' + record.productCode;
          },
        },
        {
          title: '标的名称',
          key: 'securityName',
        },
        {
          title: '持仓占比',
          key: 'holdRatio',
          type: 'rate',
          width: 80,
        },
        {
          title: '债券期初评级',
          key: 'startRate',
          width: 110,
        },
        {
          title: '债券期末评级',
          key: 'endRate',
          width: 110,
        },
      ];
      if (res && res.code === 0) {
        callback(columns, res.data);
      } else {
        callback(columns, []);
        message.error(res?.msg || '获取评级下调数据失败');
      }
    },

    // 成交偏移度
    *getTransDeviationData({ payload, callback }, { call, put }) {
      const res = yield call(api.investmentTips.getTransDeviationData, payload);
      let columns = [
        {
          title: '产品名称',
          key: 'productName',
          render: (text, record) => {
            return record.productName + '-' + record.productCode;
          },
        },
        {
          title: '标的名称',
          key: 'securityName',
        },
        {
          title: '持仓占比',
          key: 'holdRatio',
          type: 'rate',
          width: 80,
        },
        {
          title: '收益率偏离度',
          key: 'degree',
          width: 110,
        },
        {
          title: '净价偏离度',
          key: 'price',
          width: 100,
        },
      ];
      if (res && res.code === 0) {
        callback(columns, res.data);
      } else {
        callback(columns, []);
        message.error(res?.msg || '获取成交偏移度数据失败');
      }
    },

    // 行为事件
    *getBehaviorData({ payload, callback }, { call, put }) {
      const res = yield call(api.investmentTips.getBehaviorData, payload);
      let columns = [
        {
          title: '产品名称',
          key: 'productName',
        },
        {
          title: '标的名称',
          key: 'securityName',
          render: (text, record) => {
            return record.securityName + '-' + record.securityCode;
          },
        },
        {
          title: '持仓占比',
          key: 'holdRatio',
          type: 'rate',
          width: 80,
        },
        {
          title: '行为事件类型',
          key: 'type',
          width: 110,
        },
      ];
      if (res && res.code === 0) {
        callback(columns, res.data);
      } else {
        callback(columns, []);
        message.error(res?.msg || '获取行为事件数据失败');
      }
    },

    // 登记事件
    *getRegisterData({ payload, callback }, { call, put }) {
      const res = yield call(api.investmentTips.getRegisterData, payload);
      let columns = [
        {
          title: '产品名称',
          key: 'productName',
        },
        {
          title: '标的名称',
          key: 'securityName',
          render: (text, record) => {
            return record.securityName + '-' + record.securityCode;
          },
        },
        {
          title: '持仓占比',
          key: 'holdRatio',
          type: 'rate',
          width: 80,
        },
        {
          title: '登记事件类型',
          key: 'type',
          width: 110,
        },
      ];
      if (res && res.code === 0) {
        callback(columns, res.data);
      } else {
        callback(columns, []);
        message.error(res?.msg || '获取登记事件数据失败');
      }
    },

    // 获取表格数据
    *getRulesResultDta({ payload, callback }, { call, put, select }) {
      const res = yield call(api.monitoring.getTableDta, payload);
      if (res && res.code === 0) {
        let num1 = 0;
        let num2 = 0;
        res.data.dataSource &&
          res.data.dataSource.map((item) => {
            if (item.warningValue && item.marginValue <= 0) {
              num1++;
            }
            if (item.prohibitValue && item.marginValue <= 0) {
              num2++;
            }
          });
        callback(num1, num2);
      } else {
        message.error(res?.msg || '获取投资限制数据失败');
      }
    },
  },

  reducers: {
    setState(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },
  },
};
